<?php
class KehoachesController extends AppController {

	var $name = 'Kehoaches';
    var $uses=array('Kehoach','Nhanvien','Tempnhiemvunv','Thoigian','Nvngay','Thoigian');

	function index($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    function xemkqcn($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
 
		$this->Kehoach->recursive = 0;
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
                 
        $this->set(compact('tendv','donvi'));
	}
    
    function indexnv($donvi=null) {
	   
       
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         print_r($tendv);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    //chi nhanh hoac trung tam giao chi tieu
    function giaochitieu($donvi=null)
    {
		$nam = $this->Kehoach->find('list',array(
	    										'conditions'=>array('donvi_id'=>$donvi),
	    										'fields'=>array('ngaybd'),
	    										'group'=>array('YEAR(Kehoach.ngaybd)'),
	    										'order'=>array('ngaybd DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
    	$namht = date('Y');	   
	    $tendvs=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
                    //debug($tendv[0]['donvis']['tendv']);
         //print_r($dulieu);
        $this->set(compact('nams','tendvs','madv','donvi','dulieu'));
	}

	function laygiaochitieu($donvi=null)
	{
		$this->layout='ajax';
    	$namht = $this->data['Kehoaches']['nam'];	   
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));     
        $this->set(compact('nams','tendvs','madv','donvi','dulieu'));
		
	}
    //trung tam lap ke hoach tac nghiep
    function lapkehoach($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    //trung tam theo doi ket qua thuc hien cong viec cua nhan vien
    function theodoikehoach($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    
     //trung tam, phong ban theo doi viec tao khtuan
    function khtuan($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    
    //Nhan vien báo cáo ket qua ban hang
    function ketquabanhang($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    function ketquachitieu($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
    
    //nhan vien bao cao ket qua thuc hien cong viec
    function ketquacongviec($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
   
   //theo doi ket qua ban hang nhan vien, trungt tâm
    function theodoibanhang($donvi=null) {
	   
	    $tendv=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('donvi_id'=>$donvi)));
        
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
         //print_r($dulieu);          
        $this->set(compact('tendv','madv','donvi','dulieu'));
	}
	
	function theodoichitieu()
	{
		$nhanvien_id = $this->Session->read("cur_manv");
		$nam = $this->Kehoach->Chitieunv->find('list',array(
												'recursive'=>0,
	    										'conditions'=>array('nhanvien_id'=>$nhanvien_id),
	    										'fields'=>array('Kehoach.ngaybd'),
	    										'group'=>array('YEAR(Kehoach.ngaybd)'),
	    										'order'=>array('ngaybd'=>'DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
		$namht = date('Y');
		$kehoach = $this->Kehoach->Chitieunv->find('list',array(
																'conditions'=>array('nhanvien_id'=>$nhanvien_id),
																'fields'=>array('kehoach_id'),
																'group'=>array('kehoach_id')));
		$this->Kehoach->recursive = 0;
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('Kehoach.id'=>$kehoach,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
		//tuan
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('Kehoach.id'=>$kehoach,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
		$nhanvien = $this->Kehoach->Chitieunv->Nhanvien->find('list',array('conditions'=>array('id'=>$nhanvien_id)));
		$this->set(compact('nhanvien','nams'));
	}

	function laytheodoict()
	{
		$this->layout='ajax';
		$nhanvien_id = $this->Session->read("cur_manv");
		$namht = $this->data['Kehoaches']['nam'];
				$kehoach = $this->Kehoach->Chitieunv->find('list',array(
																'conditions'=>array('nhanvien_id'=>$nhanvien_id),
																'fields'=>array('kehoach_id'),
																'group'=>array('kehoach_id')));
		$this->Kehoach->recursive = 0;
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('Kehoach.id'=>$kehoach,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
		//tuan
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('Kehoach.id'=>$kehoach,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
		$nhanvien = $this->Kehoach->Chitieunv->Nhanvien->find('list',array('conditions'=>array('id'=>$nhanvien_id)));
		$this->set(compact('nhanvien','nams'));
	}
	
	function theodoichitieudv()
	{
		$donvi_id = $this->Session->read("cur_dv");
		$dvht = $this->Thuocdv->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id),'fields'=>array('madv')));
		$ldv =substr($dvht[key($dvht)],0,2);
		if($ldv =='TT')
			$donvi_id = $this->Session->read("cur_dv");
		else
			$donvi_id = $this->Session->read("cur_cha");
		$kehoach = $this->Kehoach->Chitieudv->find('list',array('conditions'=>array('donvi_id'=>$donvi_id),
																'fields'=>array('kehoach_id'),
																'group'=>array('kehoach_id')));
		$this->Kehoach->recursive = 0;
		$this->set('kehoaches', $this->paginate(array('Kehoach.id'=>$kehoach)));
		$donvi = $this->Kehoach->Chitieudv->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id)));
		$this->set(compact('donvi','ldv'));
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid kehoach', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('kehoach', $this->Kehoach->read(null, $id));
	}

	function add($dv_id=null) {
		if (!empty($this->data)) {
			$this->data['Kehoach']['donvi_id']=$dv_id;
			$this->Kehoach->create();
            
          /** ********************************************
           * Phần lấy id căn cứ vào ngaybd va kt nhập vào
           ********************************************* */  
            $idthoigian=$this->Kehoach->Thoigian->find('first',array(
                        'conditions'=>array('Thoigian.ngaybd'=>$this->data['Kehoach']['ngaybd'],
                                            'Thoigian.ngaykt'=>$this->data['Kehoach']['ngaykt']),
                        'fields'=>array('Thoigian.id')));
            
            if(empty($idthoigian)){
                $record['Thoigian']['ngaybd']=$this->data['Kehoach']['ngaybd'];
                $record['Thoigian']['ngaykt']=$this->data['Kehoach']['ngaykt'];
                
                if($this->data['Kehoach']['checkthang']!=1){//neu thoi gian nhap vo k fai la thang
                    
                    //do xem tuan nay thuoc thang nao
                    $idtg=$this->Kehoach->query('select id from thoigians 
                                    where
                                        ngaybd='.$this->data['Kehoach']['ngaybd'].'
                                    and datediff(Thoigian.ngaybd,Thoigian.ngaykt) in (28, 29, 30, 31) ');
                    //neu thang nay chua co thi bao loi
                    if(empty($idtg)){
                        $this->Session->setFlash(__('Tháng cho tuần này chưa được tạo', true));
                        $this->redirect(array('controller'=>'kehoaches', 'action' => 'index',$dv_id));
                
                    }
                        
                    //neu tim dc thi ghi nhan vo de lat ghi vo csdl    
                    $record['Thoigian']['thoigian']=$idtg[0]['thoigians']['id'];
                }
                
                $this->Thoigian->create();
                if($this->Thoigian->saveAll($record)){
                    //lay id cua thoi gian
                    $idthoigian=$this->Thoigian->id;
                }
                else{
                    $this->Session->setFlash(__('Lỗi khi nhập thời gian', true));
                    $this->redirect(array('action' => 'chonchitieu',$dv_id));
                }
            }else{
                $idthoigian=$idthoigian['Thoigian']['id'];
            }
            $this->data['Kehoach']['thoigian_id']=$idthoigian;          
           /** ********************************************
           * HẾT 
           ********************************************* */  
			if ($this->Kehoach->save($this->data)) {
				$this->Session->setFlash(__('Kế hoạch đã được tạo', true));
                
                //them nhan vien vao bang nhiemvunv ngay khi vua tao ke hoach
                
                $id=$this->Kehoach->id;                
                $nhanviens=$this->Nhanvien->find('list',
                        array('fields'=>array('id'),
                           'conditions'=>array('donvi_id'=>$dv_id),'recursive'=>-1));
                     foreach ($nhanviens as $nv):
                        $record['Tempnhiemvunv']['nhanvien_id']=$nv;
                        $record['Tempnhiemvunv']['kehoach_id']=$id;
                        $this->Tempnhiemvunv->saveAll($record);
                endforeach;
                
                
				$this->redirect(array('controller'=>'chitieudvs', 'action' => 'chonchitieu',$this->Kehoach->id));
			} else {
				$this->Session->setFlash(__('The kehoach could not be saved. Please, try again.', true));
			}
		}
		$thoigians = $this->Kehoach->Thoigian->find('list');
        $thang=$this->Thoigian->query(" select thoigians.id, thoigians.tentg, thoigians.ngaybd, thoigians.ngaykt
                                    from thoigians
                                    where DATEDIFF(thoigians.ngaykt,thoigians.ngaybd)>8
                                    and thoigians.id NOT IN (select thoigian_id from kehoaches )
                                   ");
        $tuan=$this->Thoigian->query(" select thoigians.id, thoigians.tentg, thoigians.ngaybd, thoigians.ngaykt
                                    from thoigians
                                    where DATEDIFF(thoigians.ngaykt,thoigians.ngaybd)<8
                                    and thoigians.id NOT IN (select thoigian_id from kehoaches )
                                   ");
        //print_r($thang);
		$donvis = $this->Kehoach->Donvi->find('list');
		$this->set(compact('thoigians', 'donvis','thang','dv_id'));
	}
    
    function addcn($dv_id=null) {
    	if(!$dv_id)
    		$this->redirect(array('controller'=>'pages','action'=>'home'));
		if (!empty($this->data)) {
			$this->data['Kehoach']['donvi_id']=$dv_id;
			$this->Kehoach->create();
            $ngaybd = date('Y-m-d',strtotime($this->data['Kehoach']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Kehoach']['ngayktt']));
            /** ********************************************
           * Phần lấy id căn cứ vào ngaybd va kt nhập vào
           ********************************************* */ 
           $kt1= date('m',strtotime($this->data['Kehoach']['ngaybd']));
            $kt=$this->Thoigian->query('
                    select k.id 
                    from kehoaches k
                    where datediff(ngaykt,ngaybd)>10
                        and donvi_id='.$dv_id.' 
                        and month(k.ngaybd)=\''.$kt1.'\'');
            if(empty($kt)){
                $idthoigian=$this->Kehoach->Thoigian->find('first',array(
                        'conditions'=>array('Thoigian.ngaybd'=>$ngaybd,
                                            'Thoigian.ngaykt'=>$ngaykt),
                        'fields'=>array('Thoigian.id')));
            
                if(empty($idthoigian)){
                    $record['Thoigian']['ngaybd']=$ngaybd;
                    $record['Thoigian']['ngaykt']=$ngaykt;
                    $record['Thoigian']['thoigian_id']=0;
                                
                    $this->Thoigian->create();
                    if($this->Thoigian->saveAll($record)){
                    //lay id cua thoi gian
                        $idthoigian=$this->Thoigian->id;
                    }
                    else{
                        $this->Session->setFlash(__('Lỗi khi nhập thời gian', true));
                    //$this->redirect(array('controller'=>'chitieudvs','action' => 'chonchitieu',$dv_id));
                    }
                }else{
                    $idthoigian=$idthoigian['Thoigian']['id'];
                }
                $this->data['Kehoach']['thoigian_id']=$idthoigian;          
                $this->data['Kehoach']['ngaybd']=$ngaybd;
                $this->data['Kehoach']['ngaykt']=$ngaykt;
           /** ********************************************
           * HẾT 
           ********************************************* */
                if ($this->Kehoach->save($this->data)) {
				    $this->Session->setFlash(__('Kế hoạch đã được tạo', true));
                
                //them nhan vien vao bang nhiemvunv ngay khi vua tao ke hoach
                    $id=$this->Kehoach->id;                
                    $nhanviens=$this->Nhanvien->find('list',
                        array('fields'=>array('id'),
                                 'conditions'=>array('donvi_id'=>$dv_id),'recursive'=>-1));
                   foreach ($nhanviens as $nv):
                        $record['Tempnhiemvunv']['nhanvien_id']=$nv;
                        $record['Tempnhiemvunv']['kehoach_id']=$id;
                        $this->Tempnhiemvunv->saveAll($record);
                    endforeach;
                
                    $dv = $this->Kehoach->Donvi->find('list',array(
                        'conditions'=>array('id'=>$dv_id),
                        'fields'=>array('madv')));
                    $tt = substr($dv[$dv_id],0,2);
                    if ($tt=="CN" || $tt=="CT")
                    {
     				   $this->redirect(array('controller'=>'chitieudvs', 'action' => 'chonchitieu',$this->Kehoach->id));
                    }
                    else
	                {
	                	$this->redirect(array('controller'=>'z1chithikhs', 'action' => 'index',$dv_id));
	                }
				$this->redirect(array('controller'=>'chitieudvs', 'action' => 'chonchitieu',$this->Kehoach->id));
			} else {
				$this->Session->setFlash(__('Có thể mốc thời gian bạn nhập đã có rồi. Vui lòng nhập lại', true));
			}
            }
            else{
               	$this->Session->setFlash(__('Kế hoạch cho từ ngày '.$this->data['Kehoach']['ngaybd'].' đến ngày '.$this->data['Kehoach']['ngayktt'].' này đã tồn tại', true));
		
            }
		}
		$thoigians = $this->Kehoach->Thoigian->find('list');
        $thang=$this->Thoigian->query(" select thoigians.id, thoigians.tentg, thoigians.ngaybd, thoigians.ngaykt
                                    from thoigians
                                    where DATEDIFF(thoigians.ngaykt,thoigians.ngaybd)>8
                                    and thoigians.id NOT IN (select thoigian_id from kehoaches )
                                   ");
        
		$donvis = $this->Kehoach->Donvi->find('list');
		$this->set(compact('thoigians', 'donvis','thang','dv_id'));
	}
    
 	function addtt($dv_id=null,$loaigiao=0)
    {
		if (!empty($this->data))
		{
			$this->Kehoach->create();
            
            $kt1= date('m',strtotime($this->data['Kehoach']['ngaybd']));
            $kt=$this->Thoigian->query('
                    select k.id 
                    from kehoaches k
                    where datediff(ngaykt,ngaybd)>10
                        and donvi_id='.$dv_id.' 
                        and month(k.ngaybd)=\''.$kt1.'\'');
            if(empty($kt)||empty($this->data['Kehoach']['checkthang'])){// if 1
 
               $ngaybd = date('Y-m-d',strtotime($this->data['Kehoach']['ngaybd']));
                $ngaykt = date('Y-m-d',strtotime($this->data['Kehoach']['ngayktt']));
           /** ********************************************
           * Phần lấy id căn cứ vào ngaybd va kt nhập vào
           ********************************************* */
                $idthoigian=$this->Kehoach->Thoigian->find('first',array(
                        'conditions'=>array('Thoigian.ngaybd'=>$ngaybd,
                                            'Thoigian.ngaykt'=>$ngaykt),
                        'fields'=>array('Thoigian.id')));
                if(empty($idthoigian)) // if 2
                {
                    $record['Thoigian']['ngaybd']=$ngaybd;
                    $record['Thoigian']['ngaykt']=$ngaykt;
                    if($this->data['Kehoach']['checkthang']!=1) //if 3
            	   {
                    	$idtg = $this->Kehoach->Thoigian->find('list',array(
                    							'conditions'=>array('ngaybd <='=>$ngaybd,
                    							'ngaykt >='=>$ngaykt,
                    							'DATEDIFF(Thoigian.ngaykt,Thoigian.ngaybd) >'=>8),
                								'fields'=>array('Thoigian.id')));
                    //neu thang nay chua co thi bao loi
                	   if(empty($idtg)) //if 4
                	   {
                	       	$this->Session->setFlash(__('Tháng cho tuần này chưa được tạo', true));
                	//$this->redirect(array('controller'=>'kehoaches', 'action' => 'lapkehoach',$dv_id));
                	   }
                        
               //neu tim dc thi ghi nhan vo de lat ghi vo csdl    
                	   $record['Thoigian']['thoigian_id']=key($idtg);
            	   } //het if 3
            	else //else if 2
            	{
            		$record['Thoigian']['thoigian_id']=0;
            	} //het if 2
            	
                $this->Thoigian->create();
                if($this->Thoigian->saveAll($record))
                {
                    //lay id cua thoi gian
                    $idthoigian=$this->Thoigian->id;
                }
                else
                {
                    $this->Session->setFlash(__('Lỗi khi nhập thời gian', true));
                    $this->redirect(array('action' => 'addtt',$dv_id));
                }
            } //het if 2
            else
            {
                $idthoigian=$idthoigian['Thoigian']['id'];
            }//het else if 2
            $this->data['Kehoach']['donvi_id']=$dv_id;
            $this->data['Kehoach']['thoigian_id']=$idthoigian;
            $this->data['Kehoach']['ngaybd']=$ngaybd;
            $this->data['Kehoach']['ngaykt']=$ngaykt;
            //kiem tra kh tuan nay cu trung tam lap chua 
            $kh = $this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$dv_id,'thoigian_id'=>$idthoigian)));
           /** ********************************************
           * HẾT 
           ********************************************* */ 
           if (empty($kh))
           {
			if ($this->Kehoach->save($this->data))
			{
				$this->Session->setFlash(__('Kế hoạch đã được tạo', true));
                
                //them nhan vien vao bang nhiemvunv ngay khi vua tao ke hoach
                $id=$this->Kehoach->id;                
                $nhanviens=$this->Nhanvien->find('list',
                        array('fields'=>array('id'),
                                'conditions'=>array('donvi_id'=>$dv_id),'recursive'=>-1));
                foreach ($nhanviens as $nv):
                        $record['Tempnhiemvunv']['nhanvien_id']=$nv;
                        $record['Tempnhiemvunv']['kehoach_id']=$id;
                        $this->Tempnhiemvunv->saveAll($record);
                endforeach;
                
                if ($loaigiao==0)
                {
                	//if ($ngaykt-$ngaybd <= 7)
                    if($this->data['Kehoach']['checkthang']!=1)
                		$this->redirect(array('controller'=>'chitieunvs', 'action' => 'themchitieutuan',$id));
                	else
                		$this->redirect(array('controller'=>'chitieunvs', 'action' => 'chonchitieu',$id));
                }
              	else
              		$this->redirect(array('controller'=>'z1chithikhs', 'action' => 'index',$dv_id));
			}
			else
			{
				$this->Session->setFlash(__('Không Thể Thêm Kế Hoạch.', true));
			}
           }
           else 
           {
           		$this->Session->setFlash(__('Kế hoạch cho từ ngày '.$this->data['Kehoach']['ngaybd'].' đến ngày '.$this->data['Kehoach']['ngaykt'].' này đã tồn tại', true));
           		//$this->Session->setFlash(__('Kế Hoạch Từ Ngày '.date('d-m-Y',strtotime($this->data['Kehoach']['ngaybd'])).' Đến Ngày '.date('d-m-Y',strtotime($this->data['Kehoach']['ngaykt'])).' Đã Tồn Tại', true));
           }
           }
           else{
            $this->Session->setFlash(__('Kế hoạch tháng này đã được tạo', true));
           }
		}
		$this->set(compact('dv_id','loaigiao'));
	}
    
    function addkh($dv_id=null)
    {
		if (!empty($this->data))
		{
			$this->Kehoach->create();
            
            $ngaybd = date('Y-m-d',strtotime($this->data['Kehoach']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Kehoach']['ngaykt']));
           /** ********************************************
           * Phần lấy id căn cứ vào ngaybd va kt nhập vào
           ********************************************* */
            $idthoigian=$this->Kehoach->Thoigian->find('first',array(
                        'conditions'=>array('Thoigian.ngaybd'=>$ngaybd,
                                            'Thoigian.ngaykt'=>$ngaykt),
                        'fields'=>array('Thoigian.id')));
            if(empty($idthoigian))
            {
                $record['Thoigian']['ngaybd']=$ngaybd;
                $record['Thoigian']['ngaykt']=$ngaykt;
                $ng = array(28,29,30,31);
                $idtg = $this->Kehoach->Thoigian->find('list',array(
                    							'conditions'=>array('ngaybd <='=>$ngaybd,
                    							'ngaykt >='=>$ngaykt,
                    							'DATEDIFF(Thoigian.ngaykt,Thoigian.ngaybd)'=>$ng),
                								'fields'=>array('Thoigian.id')));
                    //neu thang nay chua co thi bao loi
                if(empty($idtg))
                {
                	$this->Session->setFlash(__('Tháng cho tuần này chưa được tạo', true));
                	//$this->redirect(array('controller'=>'kehoaches', 'action' => 'lapkehoach',$dv_id));
                }
                        
               //neu tim dc thi ghi nhan vo de lat ghi vo csdl    
                $record['Thoigian']['thoigian_id']=key($idtg);
                
                $this->Thoigian->create();
                if($this->Thoigian->saveAll($record))
                {
                    //lay id cua thoi gian
                    $idthoigian=$this->Thoigian->id;
                }
                else
                {
                    $this->Session->setFlash(__('Lỗi khi nhập thời gian', true));
                    $this->redirect(array('action' => 'addkh',$dv_id));
                }
            }
            else
            {
                $idthoigian=$idthoigian['Thoigian']['id'];
            }
            $this->data['Kehoach']['donvi_id']=$dv_id;
            $this->data['Kehoach']['thoigian_id']=$idthoigian;
            $this->data['Kehoach']['ngaybd']=$ngaybd;
            $this->data['Kehoach']['ngaykt']=$ngaykt;
            //kiem tra kh tuan nay cu trung tam lap chua 
            $kh = $this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$dv_id,'thoigian_id'=>$idthoigian)));
           /** ********************************************
           * HẾT 
           ********************************************* */ 
           if (empty($kh))
           {
			if ($this->Kehoach->save($this->data))
			{
				$this->Session->setFlash(__('Kế hoạch đã được tạo', true));
                
                //them nhan vien vao bang nhiemvunv ngay khi vua tao ke hoach
                $id=$this->Kehoach->id;                
                $nhanviens=$this->Nhanvien->find('list',
                        array('fields'=>array('id'),
                                'conditions'=>array('donvi_id'=>$dv_id)));
                foreach ($nhanviens as $nv):
                        $record['Tempnhiemvunv']['nhanvien_id']=$nv;
                        $record['Tempnhiemvunv']['kehoach_id']=$id;
                        $this->Tempnhiemvunv->saveAll($record);
                endforeach;
              	$this->redirect(array('controller'=>'kehoaches', 'action' => 'khtuan',$dv_id));
			}
			else
			{
				$this->Session->setFlash(__('Không Thể Thêm Kế Hoạch.', true));
			}
           }
           else 
           {
           		$this->Session->setFlash(__('Kế Hoạch Từ Ngày '.date('d-m-Y',strtotime($this->data['Kehoach']['ngaybd'])).' Đến Ngày '.date('d-m-Y',strtotime($this->data['Kehoach']['ngaykt'])).' Đã Tồn Tại', true));
           }
		}
		$this->set(compact('dv_id'));
	}
    
	function addttkh111($dv_id=null) {
		if (!empty($this->data)) {
			$this->data['Kehoach']['donvi_id']=$dv_id;
			$this->Kehoach->create();
            $ngaybd = date('Y-m-d',strtotime($this->data['Kehoach']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Kehoach']['ngaykt']));
           /** ********************************************
           * Phần lấy id căn cứ vào ngaybd va kt nhập vào
           ********************************************* */  
            $idthoigian=$this->Kehoach->Thoigian->find('first',array(
                        'conditions'=>array('Thoigian.ngaybd'=>$this->data['Kehoach']['ngaybd'],
                                            'Thoigian.ngaykt'=>$this->data['Kehoach']['ngaykt']),
                        'fields'=>array('Thoigian.id')));
            
            if(empty($idthoigian)){
                $record['Thoigian']['ngaybd']=$this->data['Kehoach']['ngaybd'];
                $record['Thoigian']['ngaykt']=$this->data['Kehoach']['ngaykt'];
                
                if (1){//if($this->data['Kehoach']['checkthang']!=1){//neu thoi gian nhap vo k fai la thang
                    
                    //do xem tuan nay thuoc thang nao
                    $idtg=$this->Kehoach->query('select id from thoigians 
                                    where
                                        ngaybd='.$this->data['Kehoach']['ngaybd'].'
                                    and datediff(Thoigian.ngaybd,Thoigian.ngaykt) in (28, 29, 30, 31) ');
                    //neu thang nay chua co thi bao loi
                    if(empty($idtg)){
                        $this->Session->setFlash(__('Tháng cho tuần này chưa được tạo', true));
                        $this->redirect(array('controller'=>'kehoaches', 'action' => 'lapkehoach',$dv_id));
                
                    }
                        
                    //neu tim dc thi ghi nhan vo de lat ghi vo csdl    
                    $record['Thoigian']['thoigian']=$idtg[0]['thoigians']['id'];
                }
                
                $this->Thoigian->create();
                if($this->Thoigian->saveAll($record)){
                    //lay id cua thoi gian
                    $idthoigian=$this->Thoigian->id;
                }
                else{
                    $this->Session->setFlash(__('Lỗi khi nhập thời gian', true));
                    $this->redirect(array('action' => 'chonchitieu',$dv_id));
                }
            }else{
                $idthoigian=$idthoigian['Thoigian']['id'];
            }
            $this->data['Kehoach']['thoigian_id']=$idthoigian;          
           /** ********************************************
           * HẾT 
           ********************************************* */ 
          
            if ($this->Kehoach->save($this->data)) {
				$this->Session->setFlash(__('Kế hoạch đã được tạo', true));
                
                //them nhan vien vao bang nhiemvunv ngay khi vua tao ke hoach
                $id=$this->Kehoach->id;                
                $nhanviens=$this->Nhanvien->find('list',
                        array('fields'=>array('id'),
                                'conditions'=>array('donvi_id'=>$dv_id)));
                foreach ($nhanviens as $nv):
                        $record['Tempnhiemvunv']['nhanvien_id']=$nv;
                        $record['Tempnhiemvunv']['kehoach_id']=$id;
                        $this->Tempnhiemvunv->saveAll($record);
                endforeach;
                
                $dv = $this->Kehoach->Donvi->find('list',array(
           'conditions'=>array('id'=>$dv_id),
           'fields'=>array('madv')));
                $tt = substr($dv[$dv_id],0,2);
                if ($tt=="CN")
                {
     $this->redirect(array('controller'=>'chitieudvs', 'action' => 'chonchitieu',$this->Kehoach->id));
                }
                else
                {
                 $this->redirect(array('controller'=>'kehoaches', 'action' => 'lapkehoach',$dv_id));
                }
				$this->redirect(array('controller'=>'chitieudvs', 'action' => 'chonchitieu',$this->Kehoach->id));
			} else {
				$this->Session->setFlash(__('The kehoach could not be saved. Please, try again.', true));
			}
		}
		$thoigians = $this->Kehoach->Thoigian->find('list');
        
        $tuan=$this->Thoigian->query(" select thoigians.id, thoigians.tentg, thoigians.ngaybd, thoigians.ngaykt
                                    from thoigians
                                    where DATEDIFF(thoigians.ngaykt,thoigians.ngaybd)<8
                                    and thoigians.id NOT IN (select thoigian_id from kehoaches )
                                   ");
        //print_r($thang);
		$donvis = $this->Kehoach->Donvi->find('list');
		$this->set(compact('thoigians', 'donvis','tuan','dv_id'));
	}
	   
	function edit($id = null,$donvi=null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid kehoach', true));
			$this->redirect(array('action' => 'index',$donvi));
		}
		if (!empty($this->data)) {
			if ($this->Kehoach->save($this->data)) {
				$this->Session->setFlash(__('The kehoach has been saved', true));
				$this->redirect(array('action' => 'index',$donvi));
			} else {
				$this->Session->setFlash(__('The kehoach could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Kehoach->read(null, $id);
		}
		$thoigians = $this->Kehoach->Thoigian->find('list');
		$donvis = $this->Kehoach->Donvi->find('list');
		$this->set(compact('thoigians', 'donvis','donvi'));
	}

	function delete($id = null,$donvi=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for kehoach', true));
			$this->redirect(array('action'=>'index',$donvi));
		}
		if ($this->Kehoach->delete($id)) {
			$this->Session->setFlash(__('Kehoach deleted', true));
			$this->redirect(array('action'=>'index',$donvi));
		}
		$this->Session->setFlash(__('Kehoach was not deleted', true));
		$this->redirect(array('action' => 'index',$donvi));
	}

	function chitieudv($donvi_id = null)
	{
		if($donvi_id==null)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		else 
		{
			$nam = $this->Kehoach->find('list',array(
	    										'conditions'=>array('donvi_id'=>$donvi_id),
	    										'fields'=>array('ngaybd'),
	    										'group'=>array('YEAR(Kehoach.ngaybd)'),
	    										'order'=>array('ngaybd DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
	    	$namht = date('Y');	   
		    $tendvs=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi_id);
	        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi_id);
			$this->Kehoach->recursive = 0;
			//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
			$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
			$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi_id,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
	        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
			$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi_id,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
	        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
	        $dulieu=$this->Nvngay->find('list',array(
	                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
	                    ));
	        $this->set(compact('nams','tendvs','madv','donvi_id','dulieu'));
		}
	}
	
	function laychitieudv($donvi)
	{
		$this->layout='ajax';
    	$namht = $this->data['Kehoaches']['nam'];	   
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));     
        $this->set(compact('nams','tendvs','madv','donvi','dulieu'));
	}

	function theodoilk($donvi=null)
	{
		$nam = $this->Kehoach->find('list',array(
	    										'conditions'=>array('donvi_id'=>$donvi),
	    										'fields'=>array('ngaybd'),
	    										'group'=>array('YEAR(Kehoach.ngaybd)'),
	    										'order'=>array('ngaybd DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
    	$namht = date('Y');	   
	    $tendvs=$this->Kehoach->query("select tendv from donvis where donvis.id=".$donvi);
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));
                    //debug($tendv[0]['donvis']['tendv']);
         //print_r($dulieu);
        $this->set(compact('nams','tendvs','madv','donvi','dulieu'));
	}

	function laytheodoilk($donvi=null)
	{
		$this->layout='ajax';
    	$namht = $this->data['Kehoaches']['nam'];	   
        $madv=$this->Kehoach->query("select left(madv,2) as ma from donvis where donvis.id=".$donvi);
		$this->Kehoach->recursive = 0;
		//'group'=>array('Kehoach.ngaybd','Kehoach.ngaykt'),
		$this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >='=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoach.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('donvi_id'=>$donvi,'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>7,'YEAR(Kehoach.ngaybd)'=>$namht)));
        $t=$this->Kehoach->find('list',array('conditions'=>array('donvi_id'=>$donvi)));
        $dulieu=$this->Nvngay->find('list',array(
                    'conditions'=>array('kehoach_id'=>$t),'fields'=>array('kehoach_id')
                    ));     
        $this->set(compact('nams','tendvs','madv','donvi','dulieu'));
	}

	function suakehoach($id=null)
	{
		if(!$id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		if(!empty($this->data))
		{
		}
		if (empty($this->data))
		{
			$this->Kehoach->read(null,$id);
			$this->data['Kehoach']['ngaybd'] = date('d-m-Y',strtotime($this->data['Kehoach']['ngaybd']));
			$this->data['Kehoach']['ngaykt'] = date('d-m-Y',strtotime($this->data['Kehoach']['ngaykt'])); 
		}
		
	}
}
?>